/**
 * 
 */
package tarena.netctoss.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;

import tarena.netctoss.dao.Pagination;
import tarena.netctoss.dao.RightCondition;
import tarena.netctoss.dao.RightDao;
import tarena.netctoss.po.Right;

/**
 * 
 * @author Wangpeihu
 * @team [Never give up]
 * @since JDK1.6(建议)
 * @date 2010-3-10
 */

public class RightDaoImp implements RightDao {

	private HibernateTemplate hibernateTemplate;

	/*
	 * (non-Javadoc)
	 * 
	 * @see tarena.netctoss.dao.RightDao#delete(tarena.netctoss.po.Right)
	 */
	@Override
	public void delete(Right right) {
		hibernateTemplate.delete(right);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see tarena.netctoss.dao.RightDao#findAll()
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Right> findAll() {
		return hibernateTemplate.find("from Right");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see tarena.netctoss.dao.RightDao#findAll(tarena.netctoss.dao.Pagination)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Right> findAll(final Pagination page) {
		return hibernateTemplate.executeFind(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery("from Right order by name");
				query.setFirstResult((page.getPage() - 1) * page.getPageSize());
				query.setMaxResults(page.getPageSize());
				return query.list();
			}
		});
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see tarena.netctoss.dao.RightDao#findAll(tarena.netctoss.dao.Pagination,
	 * tarena.netctoss.dao.RightCondition)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Right> findAll(final Pagination page, final RightCondition rc) {
		return hibernateTemplate.executeFind(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria c = getRightCriteria(session, rc);
				c.setFirstResult((page.getPage() - 1) * page.getPageSize());
				c.setMaxResults(page.getPageSize());
				return c.list();
			}
		});
	}

	/**
	 * Right查询条件<br>
	 * 
	 * @param session
	 * @param rc
	 * @return
	 */
	private Criteria getRightCriteria(Session session, RightCondition rc) {
		Criteria c = session.createCriteria(Right.class);
		c.createAlias("module", "m");
		c.createAlias("operation", "o");
		// 模块名称:
		if (null != rc.getModuleName() && !"".equals(rc.getModuleName())) {
			c.add(Restrictions.eq("m.name", rc.getModuleName()));
		}
		// 权限名称:
		if (null != rc.getRightName() && "".equals(rc.getRightName())) {
			c.add(Restrictions.eq("name", "%" + rc.getRightName() + "%"));
		}
		return c;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see tarena.netctoss.dao.RightDao#findById(java.lang.Integer)
	 */
	@Override
	public Right findById(Integer id) {
		return (Right) hibernateTemplate.get(Right.class, id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * tarena.netctoss.dao.RightDao#loadMaxPage(tarena.netctoss.dao.Pagination,
	 * tarena.netctoss.dao.RightCondition)
	 */
	@Override
	public void loadMaxPage(final Pagination page, final RightCondition rc) {
		hibernateTemplate.execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria c = getRightCriteria(session, rc);
				// 添加多行函数查询
				// select count(*) from Pricing where name like ....
				c.setProjection(Projections.rowCount());
				Object o = c.uniqueResult();
				// hibernate 低版本count(*)返回 Integer
				// hibernate 高版本count(*)返回 Long
				int rowCount = 0;
				if (o instanceof Long) {
					rowCount = ((Long) o).intValue();
				} else {
					rowCount = ((Integer) o).intValue();
				}
				int maxPage = (rowCount + page.getPageSize() - 1)
						/ page.getPageSize();
				page.setMaxPage(maxPage);
				return null;
			}
		});
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see tarena.netctoss.dao.RightDao#save(tarena.netctoss.po.Right)
	 */
	@Override
	public void save(Right right) {
		hibernateTemplate.save(right);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see tarena.netctoss.dao.RightDao#update(tarena.netctoss.po.Right)
	 */
	@Override
	public void update(Right right) {
		hibernateTemplate.update(right);

	}

	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

}
